package com.gec.mall.controller;

import com.gec.mall.pojo.PageReq;
import com.gec.mall.pojo.PageResp;
import com.gec.mall.pojo.RespBean;
import com.gec.mall.pojo.TbSpecification;
import com.gec.mall.pojopro.Specification;
import com.gec.mall.service.SpecificationService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: li
 * @Date: 2023/1/8
 * @PACKAGE_NAME:com.gec.mall.controller
 * @Project_Name:mall-parent
 * @VERSION: 1.0.0
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    @GetMapping("/findSpecificationByPage")
    public PageResp findSpecificationByPage(PageReq pageReq){
        PageHelper.startPage(pageReq.getPage(), pageReq.getSize());
        Page<TbSpecification> page = (Page<TbSpecification>) specificationService.findAll();
        PageResp pageResp = new PageResp();
        pageResp.setList(page.getResult());
        pageResp.setTotal(page.getTotal());
        return pageResp;
    }

    @GetMapping("/findAllSpec")
    public RespBean findSpecif(){
        return RespBean.ok("..", specificationService.findAll());
    }

    @GetMapping("/findSpecById/{id}")
    public RespBean findSpecById(@PathVariable Long id){
        Specification specification = specificationService.findById(id);
        return RespBean.ok("查询成功",specification);
    }

    @PostMapping("/saveOrUpdateSpec")
    public RespBean saveOrUpdateSpec(@RequestBody Specification specification){
        if (ObjectUtils.isEmpty(specification.getSpecification().getId())){
            specificationService.add(specification);
        }else {
            specificationService.update(specification);
        }
        return RespBean.ok("添加成功");
    }

    @DeleteMapping("/deleteSpecById/{id}")
    public RespBean deleteById(@PathVariable Long id){
        specificationService.delete(id);
        return RespBean.ok("删除成功");
    }
}
